package com.nettytime.util;
import com.alibaba.fastjson.JSON;
import com.nettytime.entity.UserSocket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.buf.HexUtils;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class TestnettyByteBuf {

    private static String GPS_TXT = "232302FE4C45575445423134374D4E3130323935320100B7160712130929010203010000000022000000000000020F000000000201010328FFFFFFFF280096FFFF050006AFA5C901EA2C4E060101000201010001010102010101070000000000000000000801010000000000010001010CE409010100013C0A00540300000000001A0000000000000100010001000100000000FFFF02000001002FEA0A7F1E6465706F7433323936302E666C79626565732E636F6D2E636E3A36363538010000001F00010000000000000000000000FB";

    ///获取要发送的内容
    private static byte[] getContent() {
        byte[] content = new byte[65];
        for (int i = 0; i < 65; i++) {
            content[i] = (byte)i;
        }
        return content;
    }

    private static ByteBuf frame() {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        buffer.writeShort(11);
        buffer.writeInt(20);
        buffer.writeByte(2);
        buffer.writeBytes("hello world".getBytes(StandardCharsets.UTF_8));
        return buffer;
    }

    private static ByteBuf getByteBufByObject(UserSocket userSocket){
        String json = JSON.toJSONString(userSocket);
        System.out.println(Unpooled.wrappedBuffer(json.getBytes()));
        return Unpooled.wrappedBuffer(json.getBytes());
    }

    private static Object getObjectByByteBuf(ByteBuf buf){
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        UserSocket user = JSON.parseObject(new String(bytes, CharsetUtil.UTF_8), UserSocket.class);
        if(null != user) {
            System.out.println(user);
        }
        return user;
    }

    private static void getBytes(){
        long ts = System.currentTimeMillis();
        for (int i = 0; i < 101; i++) {
            if (i % 10 == 0) {
                long now = System.currentTimeMillis();
                if (i > 0)
                    log.info("已发送{}条，最近10条数据发送耗时{}ms", i, now - ts);
                ts = now;
            }
            doTranspond(GPS_TXT, ts);
        }
    }

    public static void doTranspond(String hexStr, long ts) {
        byte[] data = HexUtils.fromHexString(hexStr);
        byte[] newData = new byte[data.length - 3];
        System.arraycopy(data, 2, newData, 0, newData.length);
        System.arraycopy("L0000000000000001".getBytes(), 0, newData, 2, 17);
        {
            SimpleDateFormat sp = new SimpleDateFormat("yyMMddHHmmss");
            long ymdhms = Long.parseLong(sp.format(new Date(ts)));
            int idx = 22;
            newData[idx++] = (byte) (ymdhms / 10000000000L);//年
            newData[idx++] = (byte) (ymdhms / 100000000L % 100);//月
            newData[idx++] = (byte) (ymdhms / 1000000L % 100);//日
            newData[idx++] = (byte) (ymdhms / 10000L % 100);//时
            newData[idx++] = (byte) (ymdhms / 100L % 100);//分
            newData[idx++] = (byte) (ymdhms % 100);//秒
        }
        byte[] escapeData = escape(newData);
        System.out.println(com.nettytime.util.testjinzhengclient.HexUtils.toHexString(escapeData));
    }


    /**
     * 插入标识位&转义&插入检验码
     */
    public static byte[] escape(byte[] origin) {
        byte[] escapes = new byte[origin.length + 3];
        int idx = 0;
        escapes[idx++] = 0x23;
        escapes[idx++] = 0x23;
        byte checkCode = 0x00;
        for (byte ele : origin) {
            checkCode ^= ele;
            escapes[idx++] = ele;
        }
        escapes[idx++] = checkCode;
        log.info("发送数据：" + com.nettytime.util.testjinzhengclient.HexUtils.toHexString(escapes));
        return escapes;
    }

}
